import re
import yaml
from bitcoinrpc.authproxy import JSONRPCException

from bitcoin_client import BitcoinClient


def load_config():
    with open("config.yaml", "r", encoding="utf-8") as f:
        config = yaml.safe_load(f)
    return config


def seconds_to_time_format(seconds):
    (hours, seconds) = divmod(seconds, 3600)
    (minutes, seconds) = divmod(seconds, 60)
    time_str = ''
    if hours > 0:
        hours = str(round(hours)).zfill(2)
        time_str += f'''{hours}:'''
    minutes = str(round(minutes)).zfill(2)
    seconds = str(round(seconds)).zfill(2)
    time_str += f'''{minutes}:{seconds}'''
    return time_str

password_regex = {
    'digits': re.compile('^[0-9]+$'),
    'uppercase': re.compile('^[A-Z]+$'),
    'lowercase': re.compile('^[a-z]+$'),
    'punctuation': re.compile('^[!"#$%&\\\'()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~]*$'),
    'digits+uppercase': re.compile('^(?=.*[0-9])(?=.*[A-Z])[0-9A-Z]+$'),
    'digits+lowercase': re.compile('^(?=.*[0-9])(?=.*[a-z])[0-9a-z]+$'),
    'digits+punctuation': re.compile('^(?=.*[0-9])(?=.*[!"#$%&\\\'()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~])[0-9!"#$%&\\\'()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~]+$'),
    'lowercase+uppercase': re.compile('^(?=.*[A-Z])(?=.*[a-z])[A-Za-z]+$'),
    'punctuation+uppercase': re.compile('^(?=.*[A-Z])(?=.*[!"#$%&\\\'()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~])[A-Z!"#$%&\\\'()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~]+$'),
    'lowercase+punctuation': re.compile('^(?=.*[a-z])(?=.*[!"#$%&\\\'()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~])[a-z!"#$%&\\\'()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~]+$'),
    'digits+lowercase+uppercase': re.compile('^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])[0-9A-Za-z]+$'),
    'digits+punctuation+uppercase': re.compile('^(?=.*[0-9])(?=.*[A-Z])(?=.*[!"#$%&\\\'()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~])[0-9A-Z!"#$%&\\\'()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~]+$'),
    'digits+lowercase+punctuation': re.compile('^(?=.*[0-9])(?=.*[a-z])(?=.*[!"#$%&\\\'()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~])[0-9a-z!"#$%&\\\'()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~]+$'),
    'lowercase+punctuation+uppercase': re.compile('^(?=.*[A-Z])(?=.*[a-z])(?=.*[!"#$%&\\\'()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~])[A-Za-z!"#$%&\\\'()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~]+$'),
    'digits+lowercase+punctuation+uppercase': re.compile('^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[!"#$%&\\\'()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~])[0-9A-Za-z!"#$%&\\\'()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~]+$') }

def get_password_regex(letters_type_list = None):
    regex_list = []
    for letter_type in letters_type_list:
        key = "+".join(sorted(letter_type.split("+")))
        regex_list.append(password_regex[key])
    return regex_list


def password_valid(password = None, password_regex_list = None):
    for regex in password_regex_list:
        if regex.match(password):
            return True
        return False

if __name__ == '__main__':
    prl = get_password_regex(['uppercase+lowercase', 'punctuation+uppercase+lowercase'])
    print(password_valid('Abc123!@#', prl))
